#include<bits/stdc++.h>
using namespace std;

class Solution {

public:
    int sumOfBeauties(vector<int>& nums) {
        int n = nums.size();
        vector<bool> left(n, false), right(n, false);
        int pre = nums[0];
        for(int i = 1; i < n - 1; i++){
            if(nums[i] > pre){
                left[i] = true;
                pre = nums[i];
            }
            else{
                left[i] = false;
            }
        }

        pre = nums[n - 1];
        for(int i = n - 2; i > 0; i--){
            if(nums[i] < pre){
                right[i] = true;
                pre = nums[i];
            }
            else{
                right[i] = false;
            }
        }

        int ans = 0;
        for(int i = 1; i < n - 1; i++){
            if(right[i] && left[i])ans += 2;
            else if(nums[i] > nums[i - 1] && nums[i] < nums[i + 1])ans++;
        }
        return ans;
    }
};

/**
 * 2021.9.19
 * 周赛第二题
 */